package main

import (
	"fgo/basic/http/proto"
	"fmt"
	"net"
	"sync"
)

// 解决黏包问题
// 使用自定义协议解决
func main() {
	var wg sync.WaitGroup
	conn, err := net.Dial("tcp", "127.0.0.1:7000")
	if err != nil {
		fmt.Println("dial failed, err", err)
		return
	}
	defer conn.Close()
	for i := 0; i < 100; i++ {
		wg.Add(1)
		go sender2_1(&wg, conn)
	}
	wg.Wait()
}
func sender2_1(wg *sync.WaitGroup, conn net.Conn) {
	defer wg.Done()
	words := "{\"Id\":1,\"Name\":\"golang\",\"Message\":\"message\"}"
	data, err := proto.Encode(words)
	if err != nil {
		fmt.Println("encode msg failed, err:", err)
		return
	}
	conn.Write(data)
}
